package com.example.jin.myplayer.data;

import android.content.Context;

import com.example.jin.myplayer.R;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * Created by JIN on 2017/10/20.
 */

public class PropertyBean {
    public static String[] THEMES;
    private static  String DEAFULT_THEME;
    //应用上下文
    private Context context;
    //主题
    private String theme;

    public PropertyBean(Context context){
        this.context = context;
        THEMES = context.getResources().getStringArray(R.array.theme);
        DEAFULT_THEME = THEMES[0];
        this.loadTheme();
    }

    /**读取主题，保存在configure.cfg中*/
    private void loadTheme(){
        Properties properties = new Properties();
        try{
            FileInputStream fileInputStream = context.openFileInput("configure.cfg");
            properties.load(fileInputStream);
            theme = properties.getProperty("theme").toString();
        }catch (Exception e){
            this.saveTheme(DEAFULT_THEME);
        }
    }

    /**保存主题，保存在configure.cfg中*/
    public boolean saveTheme(String theme){
        Properties properties = new Properties();
        properties.put("theme", theme);
        try{
            FileOutputStream fileOutputStream = context.openFileOutput("configure.cfg", context.MODE_PRIVATE);
            properties.store(fileOutputStream, "");
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public String getTheme(){
        return this.theme;
    }
}
